﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.UserControls
{
    class YhSoftComboBox : ComboBox
    {

        public  Dictionary<string, string> Pys=new Dictionary<string, string>();
        public  string PySelect = "";
        private void SetPy(string key, string text)
        {
            if (!Pys.Any(m => m.Key == key))
            {
                Pys.Add(key, text);
            }
        }

        private string GetPy(string pySelect)
        {
            var py=new KeyValuePair<string, string>();
            var count = 0;
            if (this.DropDownStyle == ComboBoxStyle.DropDownList)
            {
                py = Pys.FirstOrDefault(m => m.Key.Contains(pySelect));
                count = Pys.Count(m => m.Key.Contains(pySelect));
                //if (count == 1) PySelect = "";
                if (string.IsNullOrEmpty(py.Key))
                {
                    PySelect = "";
                    return "";
                }
            }
            else 
            {
                py = Pys.FirstOrDefault(m => m.Key ==pySelect);
                count = Pys.Count(m => m.Key == pySelect);
                if (string.IsNullOrEmpty(py.Key))
                {
                    return "";
                }
            }
            return Pys[py.Key];
        }

        public YhSoftComboBox()
        {
            KeyUp += KeyUps;
            Enter += Enters;
            Leave += Leaves;
        }

        private void Leaves(object sender, EventArgs e)
        {
            Font = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, 134);
        }

        private void Enters(object sender, EventArgs e)
        {
            if (Pys.Count == 0)
            {
                foreach (object item in Items)
                {
                    var text = GetItemText(item);
                    SetPy(CnToPy.CapitalCovert(text), text);
                }
            }
            Font = new Font("宋体", 10.5F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134)));
            Focus();
            //this.OnEnter(e);
        }

        private void KeyUps(object sender, KeyEventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            if (e.KeyCode == Keys.Enter)
            {
                findForm.SelectNextControl(this, true, true, true, true);
            }

            if (e.KeyCode == Keys.Subtract || e.KeyCode ==Keys.Back)
            {
                PySelect = "";
            }
            if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z))
            {
                PySelect += e.KeyCode;
            }
            if (!string.IsNullOrEmpty(PySelect) )
            {  
                var text = GetPy(PySelect);
                if (!string.IsNullOrEmpty(text)) this.Text = text;
            }
        }
    }
}
